package Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 实现Callable接口重写call方法，再用FutureTask包装成Runable,将Runable传入Thread执行线程；
 * Callable能返回执行结果
 */
public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable myCallable = new MyCallable();
        FutureTask<Integer> futureTask = new FutureTask<Integer>(myCallable);
        new Thread(futureTask).start();
        System.out.println("Thead-Name:"+Thread.currentThread().getName());
        System.out.println("执行结果："+futureTask.get().toString());
    }


    static class MyCallable implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            System.out.println("Thead-Name:"+Thread.currentThread().getName());
            Integer sum = 1;
            for (int i=0;i<100000;i++){
                sum+=i;
            }
            Thread.sleep(3*1000);
            return sum;
        }
    }
}
